package az.mecid.rssreader.utils;

import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.SyndFeedInput;

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by IntelliJ IDEA.
 * User: mega
 * Date: 27.09.2010
 * Time: 1:20:31
 * To change this template use File | Settings | File Templates.
 */
public class FeedUtils {
    public static String validURL(String url) {

        SyndFeed feed;
        try {
            URL urlFeed = new URL(url);

            SyndFeedInput input = new SyndFeedInput();

            feed = input.build(new InputStreamReader(urlFeed.openStream(), "UTF-8"));

        } catch (MalformedURLException e) {
            return null;
        } catch (IOException e) {
            return null;
        } catch (FeedException e) {
            e.printStackTrace();
            return null;
        }
        return feed.getTitle();
    }

    public static String pattern(String url){
        String s;

        if(url.endsWith("/"))
            s = url.substring(0,url.length() - 1);
        else
            s = url;

        return s;

    }
}
